import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

// Dart示例导入
import 'dart_examples/types_example.dart';
import 'dart_examples/pattern_matching_example.dart';
import 'dart_examples/control_flow_example.dart';
import 'dart_examples/functions_example.dart';
import 'dart_examples/annotations_example.dart';
import 'dart_examples/libraries_example.dart';
import 'dart_examples/classes_objects_example.dart';
import 'dart_examples/type_modifiers_example.dart';
import 'dart_examples/concurrency_example.dart';
import 'dart_examples/null_safety_example.dart';
import 'dart_examples/keywords_example.dart';

// Flutter示例导入
import 'flutter_examples/widgets_example.dart';
import 'flutter_examples/layout_example.dart';
import 'flutter_examples/state_management_example.dart';
import 'flutter_examples/user_input_example.dart';
import 'flutter_examples/network_data_example.dart';
import 'flutter_examples/local_storage_example.dart';
import 'flutter_examples/navigation_routing_example.dart';

// 综合实战App
import 'comprehensive_app/app.dart';

void main() {
  runApp(
    const ProviderScope(
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter 学习工程',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      home: const HomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 3, vsync: this);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter 学习工程'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        bottom: TabBar(
          controller: _tabController,
          tabs: const [
            Tab(icon: Icon(Icons.code), text: 'Dart 基础'),
            Tab(icon: Icon(Icons.widgets), text: 'Flutter 开发'),
            Tab(icon: Icon(Icons.rocket_launch), text: '综合实战'),
          ],
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: const [
          DartExamplesTab(),
          FlutterExamplesTab(),
          ComprehensiveAppTab(),
        ],
      ),
    );
  }
}

// Dart基础知识点Tab
class DartExamplesTab extends StatelessWidget {
  const DartExamplesTab({super.key});

  @override
  Widget build(BuildContext context) {
    final examples = [
      {'title': '1. 类型系统', 'subtitle': 'int, double, String, bool, List, Map, Set等', 'page': const TypesExample()},
      {'title': '2. 模式匹配', 'subtitle': 'switch表达式、解构模式', 'page': const PatternMatchingExample()},
      {'title': '3. 控制流程', 'subtitle': 'if/else, for, while, switch等', 'page': const ControlFlowExample()},
      {'title': '4. 函数', 'subtitle': '函数声明、箭头函数、参数类型', 'page': const FunctionsExample()},
      {'title': '5. 注解', 'subtitle': '@override, @deprecated等', 'page': const AnnotationsExample()},
      {'title': '6. 库和导入', 'subtitle': 'import, export, show, hide', 'page': const LibrariesExample()},
      {'title': '7. 类和对象', 'subtitle': '构造函数、继承、混入', 'page': const ClassesObjectsExample()},
      {'title': '8. 类型修饰符', 'subtitle': 'final, const, static等', 'page': const TypeModifiersExample()},
      {'title': '9. 并发', 'subtitle': 'async/await, Future, Stream', 'page': const ConcurrencyExample()},
      {'title': '10. 空安全', 'subtitle': '?, !, late, ??等操作符', 'page': const NullSafetyExample()},
      {'title': '11. 关键词', 'subtitle': 'var, dynamic, void等', 'page': const KeywordsExample()},
    ];

    return ListView.builder(
      itemCount: examples.length,
      padding: const EdgeInsets.all(8),
      itemBuilder: (context, index) {
        final example = examples[index];
        return Card(
          margin: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
          child: ListTile(
            leading: CircleAvatar(
              backgroundColor: Theme.of(context).colorScheme.primary,
              child: Text(
                '${index + 1}',
                style: const TextStyle(color: Colors.white),
              ),
            ),
            title: Text(example['title'] as String),
            subtitle: Text(example['subtitle'] as String),
            trailing: const Icon(Icons.arrow_forward_ios, size: 16),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => example['page'] as Widget,
                ),
              );
            },
          ),
        );
      },
    );
  }
}

// Flutter开发知识点Tab
class FlutterExamplesTab extends StatelessWidget {
  const FlutterExamplesTab({super.key});

  @override
  Widget build(BuildContext context) {
    final examples = [
      {'title': '1. Widget种类', 'subtitle': 'StatelessWidget, StatefulWidget等', 'page': const WidgetsExample()},
      {'title': '2. 布局', 'subtitle': 'Row, Column, Stack, Container等', 'page': const LayoutExample()},
      {'title': '3. 状态管理', 'subtitle': 'setState, Riverpod, 状态提升', 'page': const StateManagementExample()},
      {'title': '4. 处理用户输入', 'subtitle': 'TextField, Button, GestureDetector', 'page': const UserInputExample()},
      {'title': '5. 网络和数据', 'subtitle': 'HTTP请求、JSON解析', 'page': const NetworkDataExample()},
      {'title': '6. 本地数据和缓存', 'subtitle': 'SharedPreferences, 文件存储', 'page': const LocalStorageExample()},
      {'title': '7. 导航和路由', 'subtitle': 'Navigator, 命名路由、参数传递', 'page': const NavigationRoutingExample()},
    ];

    return ListView.builder(
      itemCount: examples.length,
      padding: const EdgeInsets.all(8),
      itemBuilder: (context, index) {
        final example = examples[index];
        return Card(
          margin: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
          child: ListTile(
            leading: CircleAvatar(
              backgroundColor: Theme.of(context).colorScheme.secondary,
              child: Text(
                '${index + 1}',
                style: const TextStyle(color: Colors.white),
              ),
            ),
            title: Text(example['title'] as String),
            subtitle: Text(example['subtitle'] as String),
            trailing: const Icon(Icons.arrow_forward_ios, size: 16),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => example['page'] as Widget,
                ),
              );
            },
          ),
        );
      },
    );
  }
}

// 综合实战App Tab
class ComprehensiveAppTab extends StatelessWidget {
  const ComprehensiveAppTab({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Padding(
        padding: const EdgeInsets.all(24.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              Icons.rocket_launch,
              size: 100,
              color: Theme.of(context).colorScheme.primary,
            ),
            const SizedBox(height: 24),
            Text(
              '综合实战 App',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            const SizedBox(height: 16),
            const Text(
              '待办事项 + 新闻列表应用',
              style: TextStyle(fontSize: 18),
            ),
            const SizedBox(height: 8),
            const Text(
              '整合 Riverpod、go_router、Dio、rxDart',
              textAlign: TextAlign.center,
              style: TextStyle(color: Colors.grey),
            ),
            const SizedBox(height: 32),
            FilledButton.icon(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const ComprehensiveApp(),
                  ),
                );
              },
              icon: const Icon(Icons.play_arrow),
              label: const Text('启动综合实战 App'),
              style: FilledButton.styleFrom(
                padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

